package com.mz.ozy.activity.c2c;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.BalanceDetailBean;
import com.mz.ozy.bean.BankListFBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.bean.ReceiveAccountBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.CodePresenter;
import com.mz.ozy.mvp.presenter.ReceiveAccountPresenter;
import com.mz.ozy.mvp.view.CodeContract;
import com.mz.ozy.mvp.view.ReceiveAccountContract;
import com.mz.ozy.utils.CodeHelper;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReceiveAccountActivity extends BaseActivity<ReceiveAccountContract.View, ReceiveAccountPresenter> implements ReceiveAccountContract.View, CodeContract.View {


    @BindView(R.id.et_card_bank)
    TextView et_card_bank;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_alipay_account)
    EditText etAlipayAccount;
    @BindView(R.id.et_wxcard_num)
    EditText etWxcardNum;
    @BindView(R.id.et_zh_account)
    EditText etZhAccount;
    @BindView(R.id.et_bank_account)
    EditText etBankAccount;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    private CodePresenter mCodePresenter;
    private CodeHelper codeHelper;
    private String id;

    public static void go(Context context, int type) {
        Intent intent = new Intent(context, ReceiveAccountActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_account);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("绑定账户");
        int type = getIntent().getIntExtra("type", 0);
        mCodePresenter = new CodePresenter(this);
        codeHelper = new CodeHelper(this, tvGetCode);
        //   mPresenter.queryAccount(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCodePresenter.detachView();
        mCodePresenter = null;
    }

    @Override
    public void saveAccountSuccess(BalanceDetailBean bean) {
        ToastUtils.showToast("保存成功");
        finish();
    }

    @Override
    public void queryAccountSuccess(ReceiveAccountBean bean) {
    }

    @Override
    public void onGetBankSuccess(BankListFBean o) {

    }

    @Override
    public void bindInfo(JSONObject bean) {

    }

    @OnClick({R.id.tv_get_code, R.id.et_card_bank, R.id.tv_account_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:

                mCodePresenter.onGetCode(this, SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, ""), CodePresenter.MSG_TYPE_PASS);
                break;
            case R.id.et_card_bank:
                Intent intent4 = new Intent(this, BankListAcvitity.class);
                startActivityForResult(intent4, 105);
                break;
            case R.id.tv_account_save:
                String releName = etRealName.getText().toString();
                String aliPay = etAlipayAccount.getText().toString();
                String wechat = etWxcardNum.getText().toString();
                String bank_id = id;
                String bank_name = etZhAccount.getText().toString();
                String bank_account = etBankAccount.getText().toString();
                String et_code = etCode.getText().toString();
              /*  if (TextUtils.isEmpty(releName)) {
                    ToastUtils.showToast("请输入真实姓名");
                    return;
                }
                if (TextUtils.isEmpty(aliPay)) {
                    ToastUtils.showToast("请输入支付宝帐号");
                    return;
                }
                if (TextUtils.isEmpty(wechat)) {
                    ToastUtils.showToast("请输入微信账号");
                    return;
                }*/
              /*  if (TextUtils.isEmpty(bank_id)) {
                    ToastUtils.showToast("请选择银行");
                    return;
                }
                if (TextUtils.isEmpty(bank_name)) {
                    ToastUtils.showToast("请输入支行名称");
                    return;
                }
                if (TextUtils.isEmpty(bank_account)) {
                    ToastUtils.showToast("请输入银行卡帐号");
                    return;
                }*/
                if (TextUtils.isEmpty(et_code)) {
                    ToastUtils.showToast("请输入验证码");
                    return;
                }
                mPresenter.saveAccount(this, releName, aliPay, wechat, bank_id, bank_name, bank_account, et_code);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == 105 && resultCode == RESULT_OK)) {
            String name = data.getStringExtra("name");
            id = data.getStringExtra("id");
            et_card_bank.setText(name);
        }

    }

    @Override
    public void onGetCodeSuccess(JSONObject jsonObject) {
        ToastUtils.showToast(getString(R.string.have_send_verify_code));
        codeHelper.start();  //开启倒计时
    }

    @Override
    public void onGetCodeFail() {
        codeHelper.reset();  //可以点击
    }

    @Override
    public void handleFailResponse(NetBean netBean) {
        super.handleFailResponse(netBean);
        onGetCodeFail();
    }

}
